package com.chuangjiangx.member.score.ddd.application;

import com.chuangjiangx.dddbase.application.Application;
import com.chuangjiangx.domain.shared.model.Enable;
import com.chuangjiangx.member.basic.ddd.application.MbrConfigApplication;
import com.chuangjiangx.member.basic.ddd.application.command.AddAndModifyMbrConfigCommand;
import com.chuangjiangx.member.score.ddd.application.command.ModifyScoreGrandTotalRuleCommand;
import com.chuangjiangx.member.score.ddd.domain.model.MbrScoreGrandTotalRule;
import com.chuangjiangx.member.score.ddd.domain.repository.MbrScoreGrandTotalRuleRepository;
import com.chuangjiangx.member.basic.ddd.domain.model.MbrScoreGrandTotalRuleScope;
import com.chuangjiangx.member.basic.ddd.domain.model.MbrScoreGrandTotalRuleType;
import com.chuangjiangx.member.basic.ddd.domain.model.MerchantId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * 积分累计操作类
 */
@Service
public class MbrScoreGrandTotalRuleApplication implements Application {

    @Autowired
    private MbrConfigApplication mbrConfigApplication;
    @Autowired
    private MbrScoreGrandTotalRuleRepository mbrScoreGrandTotalRuleRepository;

    /**
     * 修改积分累计规则
     * 有则修改,没有则新增
     */
    @Transactional(rollbackFor = Exception.class)
    public void addAndModify (ModifyScoreGrandTotalRuleCommand command) {
        MerchantId merchantId = new MerchantId(command.getMerchantId());
        MbrScoreGrandTotalRule mbrScoreGrandTotalRule = mbrScoreGrandTotalRuleRepository.fromMerchantId(merchantId);
        if(mbrScoreGrandTotalRule == null){
            mbrScoreGrandTotalRule = new MbrScoreGrandTotalRule(
                    null,
                    merchantId,
                    "每消费" + command.getAmount() + "元积" + command.getScore() + "分",
                    MbrScoreGrandTotalRuleType.PAY,
                    MbrScoreGrandTotalRuleScope.ALL,
                    command.getAmount(),
                    command.getScore(),
                    Enable.ENABLE,
                    new Date(),
                    null
            );
        }else{
            mbrScoreGrandTotalRule.update(command.getAmount(),command.getScore());
        }
        if(null == mbrScoreGrandTotalRule.getId()){
            mbrScoreGrandTotalRuleRepository.save(mbrScoreGrandTotalRule);
        }else{
            mbrScoreGrandTotalRuleRepository.update(mbrScoreGrandTotalRule);
        }

        //其他配置
        if(null != command.getSubscribePnGiftScore() || null != command.getCardConsumerGrantScore()){
            AddAndModifyMbrConfigCommand c = new AddAndModifyMbrConfigCommand();
            c.setMerchantId(command.getMerchantId());
            c.setSubscribePnGiftScore(command.getSubscribePnGiftScore());
            c.setCardConsumerGrantScore(command.getCardConsumerGrantScore());
            mbrConfigApplication.addAndmodify(c);
        }
    }

}
